<?php
require_once('include/lsamp_curl.php');
require_once('config.php');
$vs_manager = new lsamp_cURL(TRUE,'cookies.txt','gzip','',$webmin_vs_url);
if(isset($_POST['parentuser'])){
	$result = $vs_manager->post($webmin_vs_setup_url, http_build_query($_POST), $errno, $error);
	if($errno !== 0){
		die("Error: " . $error);
	}
	echo $result;
	//	preg_match('/(<html[.\n]+)/',$result, $matches);
	//	print_r($matches);
}else{
	//parse the add page to get desired controls
	$get_data="add1=" . "Add new virtual server, owned by:" . "&" . "parentuser1=" . $_GET['owner'];
	$result = $vs_manager->get($webmin_vs_add_url ."?" . $get_data, $errno, $error);
	if($errno !== 0){
		die("Error: " . $error);
	}
	$pos = strpos($result, "<select name=\"sharedip\" >");
	$tmp = substr($result, $pos, strlen($result));
	$pos = strpos($tmp, "</select>");
	$select_sharedip = substr($tmp, 0, $pos+9) ;
	$start_token =  "<input type=radio name=\"virt\"";
	$pos = strpos($result, $start_token );
	$tmp = substr($result, $pos, strlen($result));
	$end_token = "Already active</label>\n)";
	$pos = strpos($tmp, $end_token );
	$network_token = substr($tmp, 0, $pos + strlen($end_token));
	$start_token = '<a href=\'edit_domain.cgi';
	$pos = strpos($result, $start_token );
	$tmp = substr($result, $pos, strlen($result));
	$end_token = '</a>';
	$pos = strpos($tmp, $end_token );
	preg_match('/<a[^>]+>([^<]+)/',substr($tmp, 0, $pos + strlen($end_token)), $matches);
	$parentserver_token = $matches[1];

	$form = '<form action="add_vs.php" method="post">';
	$form .= '<input type="hidden" name="parentuser" value="' . $_GET['owner'] . '"/>';
	$form .= '<input type="hidden" name="prefix_def" value="prefix_def_0"/>';
	$form .= '<input type=hidden name="dir" value="1">';
	$form .= '<input type="hidden" name="mail" value="1" id="mail_1">';
	$form .= '<input type="hidden" name="dns" value="1"  checked id="dns_1" >';
	$form .= '<input type="hidden" name="web" value="1"  checked id="web_1" >';
	$form .= '<input type="hidden" name="webalizer" value="1"  checked id="webalizer_1" >';
	$form .= '<input type="hidden" name="logrotate" value="1"  checked id="logrotate_1" >';

	$table= '<table align="center">';
	$table .= '<tr><td colspan="2" align="center"><font size="5"><strong>Add domain for ' . $_GET['owner'] . '</strong></font></td></tr>';
	$table.= '<tr><td align="left" width="30%"><b>Domain name</b></td>'
	. '<td valign=top colspan=1 ><input name="dom" value="" size=50  onBlur="domain_change(this)"></td></tr>';
	$table .= '<script> function domain_change(field) {'
	. 'if (field.value.indexOf(".") < 0) {'
	. ' field.value += ".'
	.  $parentserver_token . '"'
	. '; } }</script> ';
	$table .=  '<tr><td align="left" width="30%"><b>Description</b></td><td valign=top colspan=1 ><input name="owner" value="" size=50  ></td>'
	.'</tr>';
	$table .= '<tr><td align="left" width="30%"><b>Parent server</b></td><td><lable>' . $parentserver_token . '</label></td></tr>';
	$table .= '<tr><td valign="top" width="30%"><b>Network Information</b></td>'
	.'<td valign="top" colspan="1">' . $network_token . '</td>'
	.'</tr>';
	$table .= '<tr><td align="center" colspan="2"><input type="submit" value="Add" /></td></tr>';
	$table .= '</table>';
	$form .= $table;
	$form .= '</form>';
	echo $form;

}
